// src/components/countdown/index.js
import $CountDown from '../../utils/countdown.js'
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    reaRainCount: {
      type: Object,
      value: {},
      // observer: 'countDown'
      observer(newVal, oldVal, changedPath) {
        if (newVal.startTime) {
          this.countDown()
        }
      }
    },
    sign: Number,
    sign2: Number,
    startNow: Number
  },

  /**
   * 组件的初始数据
   */
  data: {
    days: 0,
    hours: '0' + 0,
    min: '0' + 0,
    sec: '0' + 0,
    sign: String,
    timeout: '',
    timedown: ''
  },
  timer: null,
  attached: function() {},

  /**
   * 组件的方法列表
   */
  methods: {
    countDown() {
      const self = this

      if (this.timer) {
        this.timer.update(self.properties.reaRainCount.startTime)
      } else {
        this.timer = new $CountDown({
          date: new Date(self.properties.reaRainCount.startTime),
          render(date) {
            const days = this.leadingZeros(date.days, 1)
            const hours = this.leadingZeros(date.hours, 2)
            const min = this.leadingZeros(date.min, 2)
            const sec = this.leadingZeros(date.sec, 2)
            self.setData({
              days: days,
              hours: hours,
              min: min,
              sec: sec
            })
            if (days == 0 && hours == 0 && min == 0 && sec == 0) {
              // 倒计时为O重新获取时间
              self.triggerEvent('rain')
              // self.data.timeout = setInterval(() => {
              //   // 如果存在开始时间那么
              //   if (self.properties.startNow) {
              //     clearInterval(self.data.timeout)
              //   } else {
              //     self.triggerEvent('rain')
              //   }
              // }, 5000)
            }
          }
        })
      }
    },
    /**
     * 清除定时器
     */
    clearTimer() {
      if (this.timer) {
        clearTimeout(this.timer)
        this.timer = null
      }
    }
    // countTime(){
    //   var self = this;
    //   var times = self.properties.reaRainCount.time/1000
    //   var timer=null;
    //   timer=setInterval(function(){
    //     var day=0,
    //       hour=0,
    //       minute=0,
    //       second=0;//时间默认值
    //     if(times > 0){
    //       day = Math.floor(times / (60 * 60 * 24));
    //       hour = Math.floor(times / (60 * 60)) - (day * 24);
    //       minute = Math.floor(times / 60) - (day * 24 * 60) - (hour * 60);
    //       second = Math.floor(times) - (day * 24 * 60 * 60) - (hour * 60 * 60) - (minute * 60);
    //     }
    //     // if (day <= 9) day = '0' + day;
    //     if (hour <= 9) hour = '0' + hour;
    //     if (minute <= 9) minute = '0' + minute;
    //     if (second <= 9) second = '0' + second;
    //     self.setData({
    //         days:day,
    //         hours:hour,
    //         min:minute,
    //         sec:second
    //     })
    //     //
    //     times--;
    //     if(times<=0){
    //       if(self.properties.reaRainCount.startNow){
    //         clearInterval(timer)
    //       }else{
    //         self.triggerEvent('rain')
    //         clearInterval(timer)
    //       }
    //     }
    //   },1000);

    // },
    // countDown () {
    //   const self = this
    //   new $CountDown({
    //     date: new Date(self.properties.reaRainCount.startTime),
    //     render(date) {
    //       const days = this.leadingZeros(date.days, 1)
    //       const hours = this.leadingZeros(date.hours, 2)
    //       const min = this.leadingZeros(date.min, 2)
    //       const sec = this.leadingZeros(date.sec, 2)
    //       self.setData({
    //         days: days,
    //         hours: hours,
    //         min: min,
    //         sec: sec
    //       })
    //       if (days == 0 && hours == 0 && min == 0 && sec == 0) {
    //         self.data.timeout = setInterval(() => {
    //           if (self.properties.reaRainCount.startNow) {
    //             clearInterval(self.data.timeout)
    //           } else {
    //             self.triggerEvent('rain')
    //           }
    //         }, 5000)
    //       }
    //     }
    //   })
    // }
  },
  detached: function() {
    this.clearTimer()
  }
})
